package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.entity.HolidayType;
import org.example.models.R;
import org.example.service.HolidayTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 鸢一折纸
 * @Email: 3498881048@qq.com
 * @Date: 2025/08/01 16:20
 * @Description:
 **/

@RestController
@RequestMapping("/holidayType")
public class HolidayTypeController {

    @Autowired
    private HolidayTypeService holidayTypeService;

    @GetMapping("/page")
    public R<Page<HolidayType>> getAllHolidayType(
            @RequestParam(name = "current", defaultValue = "1") int current,
            @RequestParam(name = "size", defaultValue = "4") int size,
            @RequestParam(name = "leaveType", required = false) String leaveType
    ){
        QueryWrapper<HolidayType> query = new QueryWrapper<>();
        if (leaveType != null && !leaveType.isEmpty()) {
            query.lambda().like(HolidayType::getLeaveType, leaveType);
        }

        Page<HolidayType> page = new Page<>(current, size);
        holidayTypeService.page(page, query);
        return R.success(page);
    }

    @GetMapping("/list")
    public R getAllHolidayType(){
        return R.success(holidayTypeService.list());
    }

    @GetMapping("/findByLeaveType")
    public R<List<HolidayType>> findByLeaveType(String leaveType){
        return R.success(holidayTypeService.list(new QueryWrapper<HolidayType>().lambda().eq(HolidayType::getLeaveType, leaveType)));
    }

    @PostMapping("/insert")
    public R insertHolidayType(@RequestBody HolidayType holidayType){
        return holidayTypeService.save(holidayType) ? R.success("添加成功") : R.error("添加失败");
    }

    @PostMapping("/update")
    public R updateHolidayType(@RequestBody HolidayType holidayType){
        return holidayTypeService.updateById(holidayType) ? R.success("修改成功") : R.error("修改失败");
    }
}
